﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using ASPNETPerformance.App_BusinessLogic.Helpers;
using ASPNETPerformance.App_BusinessLogic.HttpModules;
using ASPNETPerformance.App_BusinessLogic.Utils;

[assembly: PreApplicationStartMethod( typeof( WebZipHttpModule ), "Register" )]

namespace ASPNETPerformance.App_BusinessLogic.HttpModules {
    public class WebZipHttpModule : IHttpModule {
        private static readonly IDictionary<string, string> _contentTypeLookup = new Dictionary<string, string>
        {
            {".css", "text/css"},
            {".js", "application/x-javascript"},
        };

        private readonly static IDictionary<string, string> _zippedFiles = new Dictionary<string, string>
        {
            {CommonHelper.RelativeWebRoot + "_assets/css/core.css", CommonHelper.RelativeWebRoot + "_assets/css/Release/zip/core.min.css.gz"},
            {CommonHelper.RelativeWebRoot + "_assets/css/CustomStyles.css", CommonHelper.RelativeWebRoot + "_assets/css/Release/zip/CustomStyles.min.css.gz"},
            {CommonHelper.RelativeWebRoot + "_assets/css/grid.css", CommonHelper.RelativeWebRoot + "_assets/css/Release/zip/grid.min.css.gz"},
            {CommonHelper.RelativeWebRoot + "_assets/css/round.css", CommonHelper.RelativeWebRoot + "_assets/css/Release/zip/round.min.css.gz"},
            {CommonHelper.RelativeWebRoot + "_assets/css/SimpleStyle.css", CommonHelper.RelativeWebRoot + "_assets/css/Release/zip/SimpleStyle.min.css.gz"},
            {CommonHelper.RelativeWebRoot + "_assets/css/sinorcaish-screen.css", CommonHelper.RelativeWebRoot + "_assets/css/Release/zip/sinorcaish-screen.min.css.gz"},
            {CommonHelper.RelativeWebRoot + "_assets/css/UglyStyle.css", CommonHelper.RelativeWebRoot + "_assets/css/Release/zip/UglyStyle.min.css.gz"},
            {CommonHelper.RelativeWebRoot + "_assets/css/Combine.all.css", CommonHelper.RelativeWebRoot + "_assets/css/Release/zip/Combine.all.min.css.gz"},
            {CommonHelper.RelativeWebRoot + "_assets/css/Public.all.css", CommonHelper.RelativeWebRoot + "_assets/css/Release/zip/Public.all.min.css.gz"},
            // Hr
            {CommonHelper.RelativeWebRoot + "_assets/js/jquery-1.4.4.min.js", CommonHelper.RelativeWebRoot + "_assets/js/Release/zip/jquery-1.4.4.min.js.gz"},
            {CommonHelper.RelativeWebRoot + "_assets/js/public.js",CommonHelper.RelativeWebRoot + "_assets/js/Release/zip/public.min.js.gz"},
            {CommonHelper.RelativeWebRoot + "_assets/js/Combine.all.js",CommonHelper.RelativeWebRoot + "_assets/js/Release/zip/Combine.all.min.js.gz"},
            {CommonHelper.RelativeWebRoot + "_assets/js/Public.all.js",CommonHelper.RelativeWebRoot + "_assets/js/Release/zip/Public.all.min.js.gz"},
        };

        public void Init( HttpApplication context ) {
            context.BeginRequest += OnBeginRequest;
        }

        private void OnBeginRequest( object sender, EventArgs e ) {
            var application = ( HttpApplication )sender;
            var request = application.Request;
            var url = request.Url.AbsolutePath;
            var extension = Path.GetExtension( url );
            if ( 
                request.Headers[ "Accept-Encoding" ] != null &&
                request.Headers[ "Accept-Encoding" ].Contains( "gzip" ) &&
                _contentTypeLookup.ContainsKey( extension ) &&
                IsZipped( url ) &&
                CommonHelper.GetCookieBool( "IsWebZip" ) 
            ) {
                var response = application.Response;
                response.AddHeader( "Content-Encoding", "gzip" );
                response.AddHeader( "Content-Type", _contentTypeLookup[ extension ] );
                response.WriteFile( _zippedFiles[ url ] );
                application.CompleteRequest();
            }
        }

        private static bool IsZipped( string url ) {
            return _zippedFiles.ContainsKey( url );
        }

        public void Dispose() {
        }

        public static void Register() {
            Global.RegisterModule( delegate( HttpApplication app ) {
                return new WebZipHttpModule();
            } );
            Global.RegisterModule( delegate( HttpApplication app ) {
                return new WebCacheHttpModule();
            } );
            Global.RegisterModule( delegate( HttpApplication app ) {
                return new WebImageHttpModule();
            } );
            Global.RegisterModule( delegate( HttpApplication app ) {
                return new HttpCompressionModule();
            } );
            Global.RegisterModule( delegate( HttpApplication app ) {
                return new DiskCacheHttpModule();
            } );
            Global.RegisterModule( delegate( HttpApplication app ) {
                return new SpriteImageOptimizationModule();
            } );
        }
    }
}